<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>你的游戏系统 - 首页</title>
    <!-- Bootstrap CSS 链接 -->
    <link rel="stylesheet" href="./common/css/bootstrap.min.css">
    <link rel="stylesheet" href="./common/css/index.css">
</head>

<body>

    <?php
    // 引入数据库连接代码
    include 'conn.php';
    include 'header.php';
    include 'function.php';

    // 获取类别ID和名称
    $category_id = $_GET['category_id'];
    $category_name = getCategoryNameById($conn, $category_id); // 自定义函数，根据类别ID获取类别名称

    // 分页相关代码
    $gamesPerRow = 4; // 每行显示的游戏数量
    $rowsPerPage = 2; // 每页显示的行数
    $currentPage = isset($_GET['page']) ? $_GET['page'] : 1;

    // 查询特定类别的游戏信息，加上 LIMIT 子句实现分页
    $result = getGamesByCategory($conn, $category_id, $gamesPerRow, $rowsPerPage, $currentPage);

    if ($result && mysqli_num_rows($result) > 0) {
        echo '<div class="container mt-4">';
        echo '<h2>所有（' . $category_name . '）</h2>';

        // 游戏卡片放入水平滚动的容器
        echo '<div class="row">';

        while ($row = mysqli_fetch_assoc($result)) {
            $game_id = $row['game_id'];
            $title = $row['title'];
            $description = $row['description'];
            $rating = $row['rating'];
            $image_path = $row['image_path'];

            echo '<div class="col-md-3 mb-4">';
            echo '<a href="games_details.php?game_id=' . $game_id . '" class="text-decoration-none">';
            echo '<div class="card card-hover" style="width: 18rem;">';
            echo '<img src="' . $image_path . '" class="card-img-top" alt="' . $title . '" style="height: 200px; object-fit: cover;">';
            echo '<div class="card-body">';
            echo '<h5 class="card-title">' . $title . '</h5>';
            echo '<p class="card-text description">' . $description . '</p>';
            echo '<p class="card-text"><small class="text-muted">评分: ' . $rating . '</small></p>';
            echo '</div>';
            echo '</div>';
            echo '</a>';
            echo '</div>';
        }

        echo '</div>'; // 关闭水平滚动的容器
        echo '</div>';

        // 分页按钮
        echo '<div class="mt-4">';
        echo '<ul class="pagination justify-content-center">';
        for ($i = 1; $i <= getTotalPages($conn, $category_id, $gamesPerRow, $rowsPerPage); $i++) {
            echo '<li class="page-item ' . ($currentPage == $i ? 'active' : '') . '">';
            echo '<a class="page-link" href="?category_id=' . $category_id . '&page=' . $i . '">' . $i . '</a>';
            echo '</li>';
        }
        echo '</ul>';
        echo '</div>';

        mysqli_free_result($result);
    } else {
        // 处理未找到游戏的情况
        echo '未找到游戏信息';
    }

    // 关闭数据库连接
    mysqli_close($conn);

    // 获取特定类别的游戏信息
    function getGamesByCategory($conn, $category_id, $gamesPerRow, $rowsPerPage, $currentPage)
    {
        $startIndex = ($currentPage - 1) * $gamesPerRow * $rowsPerPage;
        $sql = "SELECT * FROM games WHERE category_id = $category_id LIMIT $startIndex, " . ($gamesPerRow * $rowsPerPage);
        return executeQuery($conn, $sql);
    }

    // 获取类别名称通过ID
    function getCategoryNameById($conn, $category_id)
    { 
        $category_id = mysqli_real_escape_string($conn, $category_id);

        $sql = "SELECT name FROM categories WHERE category_id = $category_id";
        $result = executeQuery($conn, $sql);

        if ($result && mysqli_num_rows($result) > 0) {
            $row = mysqli_fetch_assoc($result);
            return $row['name'];
        }
        return "未知类别"; // 如果未找到类别，可以返回一个默认值
    }

    // 获取特定类别的游戏总数
    function getTotalGamesByCategory($conn, $category_id)
    {
        $category_id = mysqli_real_escape_string($conn, $category_id);

        $sql = "SELECT COUNT(*) AS total FROM games WHERE category_id = $category_id";
        $result = executeQuery($conn, $sql);

        if ($result && mysqli_num_rows($result) > 0) {
            $row = mysqli_fetch_assoc($result);
            return $row['total'];
        }
        return 0;
    }

    // 获取总页数
    function getTotalPages($conn, $category_id, $gamesPerRow, $rowsPerPage)
    {
        $totalGames = getTotalGamesByCategory($conn, $category_id);
        return ceil($totalGames / ($gamesPerRow * $rowsPerPage));
    }
    ?>

</body>

</html>